package cn.kgc.searcher.service;

import cn.kgc.dto.Dto;
import cn.kgc.searcher.vo.CityVo;
import cn.kgc.searcher.vo.HotelVo;
import cn.kgc.searcher.vo.PageUtil;
import cn.kgc.searcher.vo.SolrHotelBean;
import cn.kgc.util.DtoUtil;
import com.github.pagehelper.StringUtil;
import net.bytebuddy.dynamic.scaffold.MethodGraph;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*private String keywords;
private Integer hotelLevel;
private String featureIds;
private String destination;
private Long checkOutDate;
private Long checkInDate;
private Double minPrice;
private Double maxPrice;
private String ascSort;
private String descSort;*/
@Service
public class SearchService {
    @Resource
    private SolrTemplate solrTemplate;

    public Dto<PageUtil> searchItripHotelPage(HotelVo hotelVo){
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");
        if (!StringUtil.isEmpty(hotelVo.getKeywords())){
            criteria=criteria.and("keyword").contains(hotelVo.getKeywords());
        }
        if (!(hotelVo.getHotelLevel()==null)){
            criteria=criteria.and("hotelLevel").is(hotelVo.getHotelLevel());
        }
        if (!StringUtil.isEmpty(hotelVo.getFeatureIds())){
            criteria=criteria.and("featureIds").contains(hotelVo.getFeatureIds());
        }
        if (!StringUtil.isEmpty(hotelVo.getDestination())){
            criteria=criteria.and("redundantCityName").contains(hotelVo.getDestination());
        }
        if (hotelVo.getMinPrice()!=null&& hotelVo.getMaxPrice()!=null && hotelVo.getMaxPrice()!=0){
           criteria=criteria.and("minPrice").between(hotelVo.getMinPrice(),hotelVo.getMaxPrice());
        }
        if (!StringUtil.isEmpty(hotelVo.getAscSort())){
            query.addSort(new Sort(new Sort.Order(Sort.Direction.ASC,hotelVo.getAscSort())));
        }
        if (!StringUtil.isEmpty(hotelVo.getDescSort())){
            query.addSort(new Sort(new Sort.Order(Sort.Direction.DESC,hotelVo.getDescSort())));
        }
        query.addCriteria(criteria);
        query.setRows(6);
        ScoredPage<SolrHotelBean> page=solrTemplate.queryForPage("hotel",query,SolrHotelBean.class);
        PageUtil pageUtil=new PageUtil();
        pageUtil.setRows(page.getContent());
        pageUtil.setPageCount(page.getTotalPages());
        pageUtil.setTotal(page.getTotalElements());
        pageUtil.setPageSize(10);
        return DtoUtil.getSuccesWithData(pageUtil);
    }

    public Dto<PageUtil> searchItripHotelListByHotCity(CityVo cityVo) {
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");
        if (cityVo.getCityId()!=null&&cityVo.getCityId()!=0){
            criteria=criteria.and("cityId").is(cityVo.getCityId());
        }
        if (cityVo.getCount()!=null&&cityVo.getCount()!=0){
            query.setRows(cityVo.getCount());
        }
        query.addCriteria(criteria);
        ScoredPage<SolrHotelBean> page=solrTemplate.queryForPage("hotel",query,SolrHotelBean.class);
        return DtoUtil.getSuccesWithData(page.getContent());
    }
}
